//
//  RegisterController.m
//  mangoClient
//
//  Created by dazhou on 16/5/17.
//  Copyright © 2016年 dazhou. All rights reserved.
//

#import "RegisterController.h"
#import "NoticeViewController.h"
#import "LoginController.h"
@interface RegisterController ()<UITextFieldDelegate,UIAlertViewDelegate>

@property (weak, nonatomic) IBOutlet UITextField *MobTextFiled;
@property (weak, nonatomic) IBOutlet UITextField *CodeTextFiled;
@property (weak, nonatomic) IBOutlet UITextField *PassTextFiled;
@property (weak, nonatomic) IBOutlet UITextField *AgainTextFiled;
@property (weak, nonatomic) IBOutlet UIButton *SaveBut;
@property (weak, nonatomic) IBOutlet UIButton *ImgBut;
@property (weak, nonatomic) IBOutlet UIButton *EyeBut;
@property (weak, nonatomic) IBOutlet UIButton *CodeBut;

@property (weak, nonatomic) NSTimer * timer;
@property (assign, nonatomic) int count;
@end

@implementation RegisterController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"注册";
    
    UIButton *backbutton = [[UIButton alloc]initWithFrame:CGRectMake(10, 20, 15, 25)];
    [backbutton setBackgroundImage:[UIImage imageNamed:@"香草园地-发布信息_03@2x"] forState:UIControlStateNormal];
    [backbutton addTarget:self action:@selector(goBacktoUp) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithCustomView:backbutton];
    self.navigationItem.leftBarButtonItem = backItem;

    _MobTextFiled.delegate = self;
    _CodeTextFiled.delegate = self;
    _PassTextFiled.delegate = self;
    _AgainTextFiled.delegate = self;
    
    [_MobTextFiled addTarget:self action:@selector(textValueChanged:) forControlEvents:UIControlEventEditingChanged];
    
    [_CodeTextFiled addTarget:self action:@selector(textValueChanged:) forControlEvents:UIControlEventEditingChanged];
    
    [_PassTextFiled addTarget:self action:@selector(textValueChanged:) forControlEvents:UIControlEventEditingChanged];
    
    [_AgainTextFiled addTarget:self action:@selector(textValueChanged:) forControlEvents:UIControlEventEditingChanged];
}
-(void)goBacktoUp
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)textValueChanged:(UITextField *)sender {
    
    if (_MobTextFiled.text.length==11&&_CodeTextFiled.text.length==4&&_PassTextFiled.text.length>=6&&_AgainTextFiled.text.length>=6) {
        
        _SaveBut.backgroundColor = UIColorFromRGBA(0x3cb83a, 1);
        
        _SaveBut.userInteractionEnabled = YES;
        [_ImgBut setImage:[UIImage imageNamed:@"立即注册2017-07-21_15--"] forState:UIControlStateNormal];
        
    } else if (_MobTextFiled.text.length<11||_CodeTextFiled.text.length<4||_PassTextFiled.text.length<6||_AgainTextFiled.text.length<6) {
        
        _SaveBut.backgroundColor = UIColorFromRGBA(0xdfdfdf, 1);
        [_ImgBut setImage:[UIImage imageNamed:@"立即注册2017-07-21_07-.png"] forState:UIControlStateNormal];
        _SaveBut.userInteractionEnabled = NO;
        
    }
}
#pragma mark 获取验证码
- (IBAction)CodeBut:(id)sender
{
    if (![self checkTelphone:_MobTextFiled.text]) {
        return;
    }
    
    NSString * hintStr = [NSString stringWithFormat:@"确定给%@发送短信验证码？", _MobTextFiled.text];
    UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"" message:hintStr delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alertView show];

}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (1 == buttonIndex) {
        //重新获取验证码
        [self resetGetCodeButtonState];
        [self sendMessageReq];
    }
}

- (void)sendMessageReq
{
    
    
}
 -(void)resetGetCodeButtonState
{
    self.count = 60;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(onTimerProcess:) userInfo:nil repeats:YES];
    _CodeBut.enabled = NO;
    _CodeBut.titleLabel.text = @"60(s)";
    _CodeBut.titleLabel.textColor = UIColorFromRGBA(0xf2a002, 1);
}
/** 定时器处理函数 */
- (void)onTimerProcess:(id)sender
{
    self.count --;
    NSString * hintTitle = [NSString stringWithFormat:@"还剩%d(s)", self.count];
    _CodeBut.titleLabel.text = hintTitle;
    
    if (self.count == 0) {
        _CodeBut.enabled = YES;
        [self.timer invalidate];
        self.timer = nil;
        _CodeBut.titleLabel.text = @"获取验证码";
    }
}
- (BOOL)checkTelphone:(NSString *)telPhone
{
    NSString * regex = @"[0-9]{11,11}";
    NSPredicate * pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL  isRight = [pred evaluateWithObject:_MobTextFiled.text];
    if (!isRight) {
        [self.view makeToast:@"请输入正确的号码"];
    }
    
    return isRight;
}
//隐藏密码
- (IBAction)EyeBut:(id)sender
{
    _EyeBut.selected = !_EyeBut.selected;
    if (_EyeBut.selected) {
        _AgainTextFiled.secureTextEntry = YES;
        return;
    }
    _AgainTextFiled.secureTextEntry = NO;

}
 //对勾
- (IBAction)Imgbut:(id)sender
{

    _ImgBut.selected = !_ImgBut.selected;
    _SaveBut.enabled = _ImgBut.selected;
    _SaveBut.backgroundColor = _SaveBut.enabled?UIColorFromRGBA(0x3cb83a, 1):UIColorFromRGBA(0xdfdfdf, 1);
}
#pragma mark 同意协议
- (IBAction)AgreementBut:(id)sender
{
    NoticeViewController* NoticeVC = [[NoticeViewController alloc]init];
    [self.navigationController pushViewController:NoticeVC animated:YES];
}
#pragma mark 保存
- (IBAction)SaveBut:(id)sender
{
    [self sendRegisterReq];
}
- (void)sendRegisterReq
{
    if ([self checkRightAll] == NO) {
        [self.view makeToast:@"请输入正确的内容"];
        return;
    }
//    [self.view showProgressMessage:[NSString stringWithFormat:@"%@中", self.titleStr]];

    

}

- (BOOL)checkRightAll
{
    if (_MobTextFiled.text.length > 0 && _PassTextFiled.text.length > 0 && _CodeTextFiled.text.length > 0) {
        return YES;
    }
    
    return NO;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches   withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}
@end
